'''
Created on Oct 8, 2010

@author: oabalbin
'''
from collections import defaultdict,deque

class myGene:
    def __init__(self, atributes):
        self.name, self.chrom, self.strand, self.txStart, self.txEnd, self.cdsStart, self.cdsEnd, \
        self.exonCount, self.exonStarts, self.exonEnds, self.proteinID, self.alignID = \
        atributes[0],atributes[1],atributes[2],int(atributes[3]),int(atributes[4]),int(atributes[5]),int(atributes[6]),int(atributes[7]),\
        map(int,deque(atributes[8].strip(',').split(','))),map(int,deque(atributes[9].strip(',').split(','))),atributes[10],atributes[11]


def read_known_genes(inputfile_name):
    
    gene_dict = defaultdict()
    inputfile = open(inputfile_name) 
    for line in inputfile:
        fields = line.strip('\n').split('\t')
        gene_dict[fields[0]] = myGene(fields) 
    
    return gene_dict
        
def read_known_isoforms(inputfile_name):
    
    isoforms_dict = defaultdict(list)
    inputfile = open(inputfile_name)
    for line in inputfile:
        fields = line.strip('\n').split('\t')
        isoforms_dict[fields[0]].append(fields[1])
    
    return isoforms_dict

def read_hugo2ucsc_names(inputfile_name):
    
    transcripts_dict = defaultdict(list)
    inputfile = open(inputfile_name)
    for line in inputfile:
        fields = line.strip('\n').split('\t')
        transcripts_dict[fields[0]].append((fields[4],fields[1]))
    
    return transcripts_dict


def get_isoform_rows(known_isoforms,gene_set_dict, hugo_names):
    '''
    It assumes that gene_set_dict is a dictionary with gene:row in a matrix of expression. gene are ucsc symbols
    '''
    isoform_dict=defaultdict(list)
    for g, isoforms_list in known_isoforms.iteritems():
        isof_row_list=[]
        for isof in isoforms_list:
            hugo, refseq = hugo_names[isof][0][0], hugo_names[isof][0][1]
            if  hugo == refseq or (hugo.startswith('SNOR') and refseq.startswith('NR')):
                print hugo, refseq, isof   
                continue
            try:
                isof_row_list.append(gene_set_dict[isof])
            except KeyError:
                continue
        # note 10-12-10: look if there is any info about the
        # characteristic isoform to put as the name as this can affect the hugo transaltion
        iso_name = ",".join(list(hugo_names[isoforms_list[0]][0])).replace(',','&')
        isoform_dict[iso_name+'&'+isoforms_list[0]] = isof_row_list
    return isoform_dict


def get_isoform_list(known_isoforms,gene_set_dict, hugo_names):
    '''
    It assumes that gene_set_dict is a dictionary with gene:row in a matrix of expression. 
    gene are ucsc symbols
    '''
    gene_names_list= ['z' for i in range(len(gene_set_dict))]
    isoform_dict=defaultdict(list)
    
    for g, isoforms_list in known_isoforms.iteritems():
        isof_row_list=[]
        for isof in isoforms_list:
            hugo, refseq = hugo_names[isof][0][0], hugo_names[isof][0][1]
            
            if  hugo == refseq or (hugo.startswith('SNOR') and refseq.startswith('NR')):
                #print hugo, refseq, isof   
                continue
            try:
                print "This the hugo name ",  hugo_names
                print "This is the isoform list ", isoforms_list[0]
                i=gene_set_dict[isof]
                iso_name = ",".join(list(hugo_names[isoforms_list[0]][0])).replace(',','&')
                gene_names_list[i] = iso_name+'&'+isoforms_list[0]
                
            except KeyError:
                continue
        # note 10-12-10: look if there is any info about the
        # characteristic isoform to put as the name as this can affect the hugo transaltion
    return gene_names_list


def get_hugo_names(gene_set_dict, hugo_names):
    '''
    It assumes that gene_set_dict is a dictionary with gene:row in a matrix of expression. 
    gene are ucsc symbols
    '''
    gene_names_list= ['z' for i in range(len(gene_set_dict))]
    
    for gen, i  in gene_set_dict.iteritems():
        try:    
            hugo, refseq = hugo_names[gen][0][0], hugo_names[gen][0][1]                
            gene_names_list[i] = hugo+'&'+refseq+'&'+gen
        except KeyError:
            print gen
            gene_names_list.pop(i)
            continue

    return gene_names_list

